#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os,sys
sys.path.append("../code/")

import pandas as pd
from stocksignal import kirogi
from stocksignal import damrey
from tusharedata import update_stock_daily
from tqdm import tqdm
from database import select

# 更新股票列表
from tusharedata import update_stock_basic
update_stock_basic()


# 更新股票日线数据
from tenacity import retry, stop_after_attempt
@retry(stop=stop_after_attempt(1000))
def do_stock():
    
    stockpath =  os.path.join("~/", "stockdate/cvs", "stock_basic" + '.csv')
    df = pd.read_csv(stockpath)
    try:
        pbar = tqdm(total = len(df.index),desc="更新股票日线数据")
        for index, row in df.iterrows():
            
            update_stock_daily(row["code"])
            dfrow = select("stockdaily",("code='%s'" % row["code"]), orderby=["date"])
            cdf = pd.DataFrame(dfrow)
            kirogi.setup(cdf,row["code"],"stock")
            damrey.setup(cdf,row["code"],"stock")
            
            pbar.set_description("更新股票日线数据"+str(row["code"]+ row["name"]))
            pbar.update(1)
        pbar.close()
    except  Exception as e:
        print(e)
        exit -1



do_stock()
